package com.uubx.service.impl;

import com.uubx.mapper.InformationMapper;
import com.uubx.pojo.Information;
import com.uubx.service.InformationService;
import com.uubx.utils.NumberUtils;
import com.uubx.vo.BatchVo;
import com.uubx.vo.InformationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class InformationServiceImpl implements InformationService {


   @Autowired
    private InformationMapper informationMapper;
 /*  @Autowired
   private SnowFlake snowFlake;*/

    @Override
    public Information queryInformationById(Long id) {

        // Information information = informationMapper.queryInformationById(id);

        return informationMapper.queryInformationById(id);
    }
    
    @Override
    public void saveInformation(Information information) {

        //查询数据库记录数
        Long aLong = queryInformationCount();
        //截取编号中的前8位
        String substring = String.valueOf(informationMapper.queryInformationLastByNumber()).substring(0,8);

        //1.判断是否有记录，若没有记录设置新增操作时的文章的编号
        //2.判断截取的编号前8位是否为当前日期，若是则设置编号+1
        //3.把当前日期替换到编号的前8位

        if(aLong==null||!substring.equals(NumberUtils.getNewDate())){
            //设置编号   获取拼接字符串后的编号
          information.setNumber(NumberUtils.getNumber( NumberUtils.getNewDate()+"0001"));

        }else {

            information.setNumber(informationMapper.queryInformationLastByNumber()+1);
        }
        informationMapper.saveInformation(information);
    }
    
    @Override
    public void updateInformation(Information information) {
        //设置默认时间
        Calendar calendar=Calendar.getInstance();
        calendar.set(0001, 01, 01);  //年月日  也可以具体到时分秒如calendar.set(2015, 10, 12,11,32,52);
        Date date=calendar.getTime();
        //判断更新操作的上下架状态 值：是、否。若上架状态编辑是：则设置下架时间为默认时间，反之类似，
        if(information.getPuttime()!=null&&information.getPuttime().getTime()>0){
            information.setRemovetime(date);
            informationMapper.updateInformation(information);
        }else if (information.getRemovetime()!=null){
            information.setPuttime(date);
            informationMapper.updateInformation(information);
        }else {
            informationMapper.updateInformation(information);
        }
    }

    @Override
    public Long queryInformationCount() {
        //Long  aLong= informationMapper.queryInformationCount();
        return informationMapper.queryInformationCount();
    }
    
    @Override
    public List<Map> queryAllInformation(InformationVo informationVo) {
        //List<Map> mapList = informationMapper.queryAllInformation(informationVo);
        return informationMapper.queryAllInformation(informationVo);
    }
    
    @Override
    public void deleteInformationById(Long id) {
     informationMapper.deleteInformationById(id);

    }

    @Override
    public Long queryInformationLastByNumber() {
        return informationMapper.queryInformationLastByNumber();
    }

    @Override
    public void BatchDeleteInformation(BatchVo batchVo) {
        informationMapper.BatchDeleteInformation(batchVo);
    }


}
